home *** CD-ROM | disk | FTP | other *** search
- /*
- ** Copyright 1994, Silicon Graphics, Inc.
- ** All Rights Reserved.
- **
- ** This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- ** the contents of this file may not be disclosed to third parties, copied or
- ** duplicated in any form, in whole or in part, without the prior written
- ** permission of Silicon Graphics, Inc.
- **
- ** RESTRICTED RIGHTS LEGEND:
- ** Use, duplication or disclosure by the Government is subject to restrictions
- ** as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- ** and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- ** successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- ** rights reserved under the Copyright Laws of the United States.
- **
- */
- #include <X11/Xlib.h>
- #include <X11/Xutil.h>
- #include <GL/glx.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <X11/keysym.h>
- #include <string.h>
-
- static int RGBattributes_DB[] = {
- GLX_RGBA,
- GLX_RED_SIZE, 1,
- GLX_GREEN_SIZE, 1,
- GLX_BLUE_SIZE, 1,
- GLX_DOUBLEBUFFER,
- None,
- };
-
- static int CIattributes_DB[] = {
- GLX_DOUBLEBUFFER,
- None,
- };
-
- enum {
- BLACK = 0,
- RED,
- GREEN,
- YELLOW,
- BLUE,
- MAGENTA,
- CYAN,
- WHITE
- };
-
- #define COLOR_OFFSET_1 16
- #define COLOR_OFFSET_2 32
-
-
- static float rgbMap[][3] = {
- {0, 0, 0},
- {1, 0, 0},
- {0, 1, 0},
- {1, 1, 0},
- {0, 0, 1},
- {1, 0, 1},
- {0, 1, 1},
- {1, 1, 1}
- };
-
- static long W = 300, H = 300;
- static Display *dpy;
- static Window window;
- static Colormap cmap;
- int doubleBuf = 1;
- int fontToggle = 0;
- GLenum drawBuffer = GL_FRONT;
- static long rgb;
- static long ci1 = BLUE, ci2 = GREEN;
-
-
- static LoadGLFont(GLuint *pBase, GLsizei *pCount)
- {
- XFontStruct *fontInfo;
- Font id;
- int first, last;
-
- static char pattern1[] = "-adobe-courier-bold-o-normal--18-*-*-*-*-*-*-*";
- static char pattern2[] = "-adobe-helvetica-*-o-normal-*-18-*-*-*-*-*-*-*";
- static char pattern3[] = "-sgi-screen-bold-*-normal-*-18-*-*-*-*-*-*-*";
- static char pattern4[] = "-sgi-rock-*-*-*-*-18-*-*-*-*-*-*-*";
-
- fontToggle = fontToggle%4;
- switch (fontToggle) {
- case 0: fontInfo = XLoadQueryFont(dpy, pattern1);
- break;
- case 1: fontInfo = XLoadQueryFont(dpy, pattern2);
- break;
- case 2: fontInfo = XLoadQueryFont(dpy, pattern3);
- break;
- case 3: fontInfo = XLoadQueryFont(dpy, pattern4);
- break;
- }
-
- id = fontInfo->fid;
- first = (int)fontInfo->min_char_or_byte2;
- last = (int)fontInfo->max_char_or_byte2;
- *pCount = last-first+1;
-
-
- *pBase = glGenLists(last+1);
- if (*pBase == 0) {
- return 0;
- }
- glXUseXFont(id, first, *pCount, (int)(*pBase+first));
- }
-
-
- static void Usage(void)
- {
- printf("Usage: font [-c]\n");
- printf(" -c: Run in color index mode\n");
- printf(" f key rotates through fonts \n");
- exit(-1);
- }
-
- static void DoDisplay(void)
- {
- GLfloat y = 5.0;
- GLuint base;
- GLsizei count;
-
- LoadGLFont(&base, &count);
- glListBase(base);
-
- glLoadIdentity();
- glOrtho(0.0, W, 0.0, H, -0.5, 1000.0);
-
- glDrawBuffer(GL_FRONT);
- glClearColor(0.0, 0.0, 0.0, 0.0);
- glClearIndex(0);
- glClear(GL_COLOR_BUFFER_BIT);
-
- glRasterPos2f(10.0, y);
- glCallLists(26, GL_UNSIGNED_BYTE,
- (unsigned char *)"abcdefghijklmnopqrstuvwxyz");
- y += 20.0;
- glRasterPos2f(10.0, y);
- glCallLists(26, GL_UNSIGNED_BYTE,
- (unsigned char *)"abcdefghijklmnopqrstuvwxyz");
- y += 20.0;
- glRasterPos2f(10.0, y);
- glCallLists(26, GL_UNSIGNED_BYTE,
- (unsigned char *)"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
- y += 20.0;
- glRasterPos2f(10.0, y);
- glCallLists(26, GL_UNSIGNED_BYTE,
- (unsigned char *)"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
- y += 20.0;
- glRasterPos2f(10.0, y);
- glCallLists(10, GL_UNSIGNED_BYTE, "0123456789");
- y += 20.0;
- glRasterPos2f(10.0, y);
- glCallLists(10, GL_UNSIGNED_BYTE, "0123456789");
-
- y += 20.0;
- glRasterPos2f(10.0, y);
- glCallLists(25, GL_UNSIGNED_BYTE,
- (unsigned char *)"N O P Q R S T U V W X Y Z");
- y += 20.0;
- glRasterPos2f(10.0, y);
- glCallLists(25, GL_UNSIGNED_BYTE,
- (unsigned char *)"! @ # $ % ^ & * ( ) _ + }");
- glXWaitGL();
-
- glFlush();
- glDeleteLists(base, count);
- }
-
- static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
- {
- if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) {
- return GL_TRUE;
- }
- return GL_FALSE;
- }
-
-
- int main(long argc, char** argv)
- {
- XVisualInfo *vi;
- XSetWindowAttributes swa;
- GLXContext cx;
- XEvent event;
- GLboolean needDisplay;
- int i;
-
- rgb = 1;
- doubleBuf = 1;
- for (i = 1; i < argc; i++) {
- if (argv[i][0] == '-') {
- switch (argv[i][1]) {
- case 'c':
- rgb = 0;
- break;
- default:
- Usage();
- }
- } else {
- Usage();
- }
- }
-
- dpy = XOpenDisplay(0);
- if (!dpy) {
- fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
- return -1;
- }
-
- vi = glXChooseVisual(dpy, DefaultScreen(dpy),
- (rgb ? RGBattributes_DB : CIattributes_DB));
- if (!vi) {
- fprintf(stderr, "No appropriate visual on \"%s\"\n",
- getenv("DISPLAY"));
- return -1;
- }
-
- cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
- rgb ? AllocNone : AllocAll);
- if (!rgb) {
- XColor buf;
- int i;
-
- buf.flags = DoRed | DoGreen | DoBlue;
-
- /* Init color map */
- for (i=0; i<16; i++) {
- buf.pixel = i;
- buf.blue = (i & 4) ? 65535 : 0;
- buf.green = (i & 2) ? 65535 : 0;
- buf.red = (i & 1) ? 65535 : 0;
- if (i > 8) {
- buf.red /= 2;
- buf.green /= 2;
- buf.blue /= 2;
- }
- XStoreColor(dpy, cmap, &buf);
- }
- }
-
-
- swa.border_pixel = 0;
- swa.colormap = cmap;
- swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
- | KeyReleaseMask;
- window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
- W, H,
- 0, vi->depth, InputOutput, vi->visual,
- CWBorderPixel|CWColormap|CWEventMask, &swa);
- XSetWMColormapWindows(dpy, window, &window, 1);
- XStoreName(dpy, window, "Fonts Test (optimized)");
- XMapWindow(dpy, window);
- XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
-
- cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
- if (!glXMakeCurrent(dpy, window, cx)) {
- fprintf(stderr, "Can't make window current to context\n");
- return -1;
- }
-
-
-
- needDisplay = GL_TRUE;
- for (;;) {
- do {
- XNextEvent(dpy, &event);
- switch (event.type) {
- case Expose:
- needDisplay = GL_TRUE;
- break;
- case ConfigureNotify:
- W = event.xconfigure.width;
- H = event.xconfigure.height;
- needDisplay = GL_TRUE;
- break;
- case KeyPress:
- {
- char buf[100];
- int rv;
- KeySym ks;
-
- rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
- switch (ks) {
- case XK_f:
- fontToggle ++;
- needDisplay = GL_TRUE;
- break;
- case XK_r:
- needDisplay = GL_TRUE;
- break;
- case XK_Escape:
- return 0;
- }
- }
- break;
- }
- } while (XPending(dpy) != 0);
-
- if (needDisplay) {
- needDisplay = GL_FALSE;
- DoDisplay();
- }
- }
- }
-